(function (angular) {
    'use strict';

    angular.module('ytong.services').factory('customerManager', customerManager);

    function customerManager(proxyService, Constants, growl, $log){

        var customers = [];

        function loadCustomers(){
            proxyService.get(Constants.URLS.CUSTOMERS_URL)
                .then(function(responseData){
                    $log.info("Feedbacks retrieved successfully.");
                    customers = responseData.customers;
                }, function(responseData){
                    if(responseData.query_result == Constants.QUERY_RESULTS.NO_RESULTS){
                        customers = [];
                        $log.info("No customers.");
                    }else {
                        $log.info("Failed retrieving customers from server: " + responseData.log_error);
                        growl.error("Failed retrieving customers");
                        customers = [];
                    }
                });
        };

        function addCustomer(customerData) {

            var customer = {};
            customer.customer = customerData;


            proxyService.addEntity(Constants.URLS.CUSTOMERS_URL,customer)
                .then(function(){
                    growl.success("Customer added");
                    loadCustomers();
                }, function(){
                    growl.error("Customer adding failed");
                });
        };

        function updateCustomer(customerData) {

            var customer = {};
            customer.customer = customerData;

            proxyService.updateEntity(Constants.URLS.CUSTOMERS_URL,customer)
                .then(function(){
                    growl.success("Customer updated");
                    loadCustomers();
                }, function() {
                    growl.error("Customer updating failed");
                });
        };

        function deleteCustomer(customerData) {

            var customer = {};
            customer.id = customerData.id;

            proxyService.deleteEntity(Constants.URLS.CUSTOMERS_URL,customer)
                .then(function(){
                    growl.success("Customer deleted");
                    loadCustomers();
                }, function(){
                    growl.error("Customer deleting failed");
                });
        }

        function getCustomers(){
            return customers;
        };

        function getCustomerName(customerId){
            for(var i=0; i< customers.length; i++){
                if(customers[i].id === customerId){
                    return customers[i].name;
                }
            };
        };

        //interface
        return{
            loadCustomers: loadCustomers,
            getCustomers: getCustomers,
            addCustomer: addCustomer,
            updateCustomer: updateCustomer,
            deleteCustomer: deleteCustomer,
            getCustomerName: getCustomerName
        };
    };

}(angular));